<template>
  <div class="box">
    <el-tabs v-model="activeTabsValue" type="card" closable @tab-remove="removeTab">
      <el-tab-pane
        v-for="(item, index) in editableTabs"
        :key="item.name"
        :label="item.title"
        :name="item.name"
      >
        {{item.content}}
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        activeTabsValue: '1111111',
        editableTabs: [{
          title: 'Tab 1',
          name: '1111111',
          content: 'Tab 1 content'
        }],
      }
    },
    methods: {
      addTab(data) {
        let newTabId = data.id;
        if (this.editableTabs.some(v => v.name === newTabId)) {
          this.activeTabsValue = newTabId;
          return
        }
        this.editableTabs.push({
          title: data.label,
          name: newTabId,
          content: data
        });
        this.activeTabsValue = newTabId;
      },
      removeTab(targetName) {
        let tabs = this.editableTabs;
        let activeName = this.activeTabsValue;
        if (activeName === targetName) {
          tabs.forEach((tab, index) => {
            if (tab.name === targetName) {
              let nextTab = tabs[index + 1] || tabs[index - 1];
              if (nextTab) {
                activeName = nextTab.name;
              }
            }
          });
        }

        this.activeTabsValue = activeName;
        this.editableTabs = tabs.filter(tab => tab.name !== targetName);
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
